
class ApiConfig{
  constructor(){
    this.apiList = {}
    this.nowModule = ''
    this.currentServerGroup = ''
    this.multiServer = false
  }
  configLoader(config) {
    for (const moduleName in config.apis) {
  
      this.setApiModule(moduleName)
      const apiMaps = config.apis[moduleName]
  
      for (const apiFunc in apiMaps) {
        if(typeof apiMaps[apiFunc] === 'string'){
          const apiUrl = apiMaps[apiFunc].split(' ')
          this.append(apiUrl[0], apiUrl[1], apiFunc)
        }else if(typeof apiMaps[apiFunc] === 'object'){
          let urls = apiMaps[apiFunc].url
          const apiUrl = urls.split(' ')
          this.appendOpt({
            ...apiMaps[apiFunc],
            method: apiUrl[0],
            url: apiUrl[1],
            call: apiFunc,
          })
  
        }
  
  
      }
    }
  
    if (config.multiServer) {
      this.openMultiServer()
      this.setServerGroup('default', config.defaultPath)
      for (let serverMap in config.servers) {
        const serverList = config.servers[serverMap]
        this.setServerGroup(serverMap, serverList.path)
        for (let apiFunc in serverList.apiUrl) {
          this.appendServerGroupApI(apiFunc, serverList.apiUrl[apiFunc])
        }
      }
    }
  }
  openMultiServer() {
    this.multiServer = true
    return this
  }
  setServerGroup(group, serverUrl) {
    this.currentServerGroup = group
    if (group === 'default') {
      for (let moduleName in this.apiList) {
        for (let apiFunc in this.apiList[moduleName]) {
          this.currentServerGroup = moduleName
          this.appendServerGroupApI(apiFunc, serverUrl)
        }
      }
      return this
    }
    const serverGroup = this.apiList[group]
    if (!serverGroup) return this
  
    for (const apiFunc in serverGroup) {
      this.appendServerGroupApI(apiFunc, serverUrl)
    }
  
    return this
  }
  appendServerGroupApI(method, serverUrl) {
    this.apiList[this.currentServerGroup][method].severUrl = serverUrl
    this.apiList[this.currentServerGroup][method].multiServer = true
    return this
  }
  appendOpt(opt){
    if (!this.nowModule) {
      throw new Error('请先使用 setModule 设置模块名')
    }
    if (!opt.call) {
      throw new Error('需要 call 设置调用方法名')
    }
    this.apiList[this.nowModule] = {
      ...this.apiList[this.nowModule],
      [opt.call]: {
        ...opt
      }
    }
  
  
  
  }
  append(method, api, call) {
    if (!this.nowModule) {
      throw new Error('请先使用 setModule 设置模块名')
    }
    if (!call) {
      throw new Error('需要 call 设置调用方法名')
    }
    this.apiList[this.nowModule] = {
      ...this.apiList[this.nowModule],
      [call]: {
        method: method,
        url: api
      }
    }
    return this
  }
  setApiModule(moudleName) {
    this.nowModule = moudleName
    if (!this.apiList[moudleName]) {
      this.apiList[moudleName] = {}
    }
    return this
  }
 
  getModuleApis(moudleName) {
    return this.apiList[moudleName]
  }
}

// const apiConfig = new ApiConfig()
module.exports= ApiConfig
